package com.oa.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonController;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.OvertimeApplyAddDTO;
import com.oa.admin.dto.OvertimeApplyListDTO;
import com.oa.admin.entity.OvertimeApply;
import com.oa.admin.service.IOvertimeApplyService;
import com.oa.admin.vo.OvertimeApplyListVO;
import com.oa.core.BaseController;
import com.oa.core.PageResult;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.system.entity.ApplyApproval;
import com.oa.system.service.IApplyApprovalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Api(description = "加班申请")
@RequestMapping("/overtimeApply")
@RestController
public class OvertimeApplyController extends BaseController {

    @Autowired
    private IOvertimeApplyService overtimeApplyService;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public Resp<PageResult<OvertimeApplyListVO>> list(@ApiParam("查询条件") OvertimeApplyListDTO overtimeApplyListDTO) {
        overtimeApplyListDTO.setUserId(getLoginUserId());
        return new Resp().success(overtimeApplyService.findOvertimeApplyList(overtimeApplyListDTO));
    }

    @GetMapping("/{id}")
    @CommonController
    @Description("根据ID查询")
    @ApiOperation(value = "查询")
    public Resp<OvertimeApplyListVO> findById(@ApiParam("申请ID") @PathVariable String id) {
        return new Resp().success(overtimeApplyService.findOvertimeApplyDetails(id));
    }


    @PostMapping("")
    @CommonController
    @Description("添加申请")
    @ApiOperation(value = "添加申请")
    public Resp add(@RequestBody OvertimeApplyAddDTO overtimeApplyAddDTO) throws BusinessException {
        Calendar calendar = Calendar.getInstance();

        Date leaveStartTime = overtimeApplyAddDTO.getStartTime();
        Date leaveEndTime = overtimeApplyAddDTO.getEndTime();

        calendar.setTime(leaveStartTime);
        Integer startTime = calendar.get(Calendar.HOUR_OF_DAY);
        Integer startWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        calendar.setTime(leaveEndTime);
        Integer endTime = calendar.get(Calendar.HOUR_OF_DAY);
        Integer endWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        if(startWeek == 0){
            startWeek = 7;
        }

        if(endWeek == 0){
            endWeek = 7;
        }

        if (!(startWeek == Calendar.SATURDAY || endWeek == Calendar.FRIDAY)) {
            return new Resp().failure("申请时间为周六周日");
        }

        double Hour = startTime - endTime;

        if (Hour > 8) {
            return new Resp().success("加班申请不能超过8个小时");
        }

        List<OvertimeApply> overtimeApplies = overtimeApplyService.selectList(
                new EntityWrapper().where("NOT ((end_time <= {0})  OR (start_time >= {1})) and user_id = {2}", overtimeApplyAddDTO.getStartTime(), overtimeApplyAddDTO.getEndTime(), getLoginUserId()));

        for (OvertimeApply overtimeApply : overtimeApplies) {
            ApplyApproval applyApproval = applyApprovalService.selectOne(new EntityWrapper().eq("apply_id", overtimeApply.getId()));
            if(applyApproval != null){
                if(!applyApproval.getApprovalNodeId().equals(0)){
                    return new Resp().failure("该时间段已存在申请");
                }
            }
        }

        return overtimeApplyService.add(getLoginUserId(), overtimeApplyAddDTO);
    }

    @PutMapping("/audit")
    @CommonController
    @Description("审批")
    @ApiOperation(value = "审批")
    public Resp audit(
            @ApiParam("申请ID") @RequestParam String id,
            @ApiParam("请假ID") @RequestParam String applyId,
            @ApiParam("备注") @RequestParam String remark,
            @ApiParam("审批结果状态") @RequestParam Integer status) throws BusinessException, DataIsEmptyException {
        return overtimeApplyService.audit(id, applyId, remark, status);
    }

    @GetMapping("/export")
    @CommonController
    @Description("导出")
    @ApiOperation(value = "导出")
    public void export(OvertimeApplyListDTO overtimeApplyListDTO, HttpServletResponse response) throws BusinessException, DataIsEmptyException {
        overtimeApplyService.export(overtimeApplyListDTO, response);
    }
}
